package script;

import api.TempApi;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class TestTemp {
    private final TempApi tempApi = new TempApi();
    private static final Logger logger = LoggerFactory.getLogger(TestLogin.class);
   //清理数据
    @BeforeClass
    public void cleaData()  {
        String sql = "DELETE FROM mb_member_info WHERE member_id in (SELECT m.id from mb_member m  WHERE m.phone = '18033727684');";
        String sql2 = "DELETE FROM mb_member  WHERE phone = '18033727684';";
        DBUtils.execute(sql);
        DBUtils.execute(sql2);
    }

    @DataProvider
    public Object[][] buildData() throws IOException {
        String keys = "phone,imgVerifyCode,type,code,description,status";
        Object[][] testData = TestDataUtil.buildData("phoneVerifyData.json", keys);
        logger.info("testData={}", testData);
        return testData;
    }

    @Test
    public void test01ImageVerity() throws Exception {
        ResponseData imageVerify = tempApi.getImageVerify(String.valueOf(new Random().nextDouble()));
        Assert.assertEquals(imageVerify.getStatusCode(),200);
    }

    @Test(dataProvider = "buildData")
    public void test02PhoneVerity(String phone,String imgVerifyCode,
                                String type,int code,String description,int status) throws Exception {
        ResponseData responseData = tempApi.getPhoneVerify(phone,imgVerifyCode,type);
        AssertUtil.commonAssert(responseData,code,description,status);
    }

    //注册
    @Test
    public void test03Regin() throws Exception {
        ResponseData responseData = tempApi.regin("18033727684","123456","8888","666666",
                "on","15958585801");
        AssertUtil.commonAssert(responseData,200,"注册成功",200);
    }

    //登录
    @Test
    public void test04Login() throws Exception {
        ResponseData responseData = tempApi.login("18033727684", "123456");
        AssertUtil.commonAssert(responseData,200,"登录成功",200);
    }

    //是否登录
    @Test
    public void test05isLogin() throws Exception {
        ResponseData responseData = tempApi.isLogin();
        AssertUtil.commonAssert(responseData,200,"OK",200);

    }
    @Test
    public void test06() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("http://39.106.49.7:1680/pdf/upload");

        // 要上传的文件
        File file = new File("C:\\Users\\39576\\Desktop\\abc.pdf");

        // 使用MultipartEntityBuilder构造请求体对象
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file", file);
        builder.addPart("userId", new StringBody("100", ContentType.MULTIPART_FORM_DATA));
        HttpEntity reqEntity = builder.build();
        httpPost.setEntity(reqEntity);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();
        System.out.println(EntityUtils.toString(resEntity, "UTF-8"));

        response.close();
        httpClient.close();
    }

    @Test
    public void test07RealName() throws Exception {
        ResponseData responseData = tempApi.realName("长老二", "1101011990d3d75699");
        AssertUtil.commonAssert(responseData,200,"提交成功!",200);
    }
    //开户
    @Test
    public void test08TustReg() throws Exception {
        ResponseData responseData = tempApi.trustReg();
        String form = responseData.getBody().getJSONObject("description").getString("form");
        logger.info("form={}",form);
        //解析html数据
        Document document = Jsoup.parse(form);
        String action = document.getElementsByTag("form").first().attr("action");
        Elements input = document.getElementsByTag("input");
        Map<String,String> params = new HashMap<>();
        for (Element e: input){
            e.attr("name");
            e.attr("value");
            params.put(e.attr("name"),e.attr("value"));
        }
        ResponseData responseData1 = HttpClientUtils.post(action, params);
        System.out.println(responseData1);
    }

}
